package jwlee.testitext;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.xhtmlrenderer.pdf.ITextRenderer;

import com.itextpdf.text.Anchor;
import com.itextpdf.text.Annotation;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class IText5Test {
	private static String path = System.getProperty("user.dir")
			+ "\\src\\jwlee\\testitext\\";

	private static void xml2pdf() {
		String filename = "weather";
		// filename = "poem";
		filename = "book";
		String inputFile = path + filename + ".xml";
		String outputFile = path + filename + ".pdf";

		try {
			OutputStream os = new FileOutputStream(outputFile);
			ITextRenderer renderer = new ITextRenderer();

			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			org.w3c.dom.Document doc = builder.parse(new FileInputStream(
					new File(inputFile)));
			renderer.setDocument(doc, null);
			// renderer.setDocument(new File(inputFile));

			renderer.layout();
			renderer.createPDF(os);
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("generate file: " + outputFile);
	}
	
	private static void generatePdf(String filename) {
		Document document = new Document();
		try {
			PdfWriter.getInstance(document, new FileOutputStream(filename));
			document.open();
			addContents(document);
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		document.close();
	}

	private static void addContents(Document document)
			throws DocumentException, IOException {
		BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
				"UniGB-UCS2-H", false);
		Font fontChinese = new Font(bfChinese);
		fontChinese.setSize(12f);
		Paragraph p1 = new Paragraph("你好！测试iText5！", fontChinese);
		document.add(p1);

		Paragraph p2 = new Paragraph(new Phrase("Paragraph 2nd: Phrase!"));
		document.add(p2);

		Paragraph p3 = new Paragraph(new Chunk("Paragraph 3rd: Chunk!",
				FontFactory.getFont(FontFactory.HELVETICA_BOLD, 12)));
		p3.add("\nAdd strings...\n");
		p3.add(new Phrase("Add phrases...\n", new Font()));
		p3.add(new Chunk("Add chunks...\n"));

		Anchor anchor = new Anchor("anchor to dc\n\n", new Font(bfChinese, 12,
				Font.UNDERLINE));
		anchor.setName("digitalchina");
		anchor.setReference("www.digitalchina.com");
		p3.add(anchor);
		Anchor anchor2 = new Anchor("inner anchor", new Font(bfChinese, 12,
				Font.UNDERLINE));
		anchor2.setReference("#digitalchina");
		p3.add(anchor2);
		p3.add("\n\n");
		document.add(p3);
		
		List list = new List(true,20);
		list.add(new ListItem("first"));
		list.add(new ListItem("second"));
		list.setNumbered(false);
		list.add(new ListItem("third"));
		document.add(list);
		
		Annotation a = new Annotation("annotation","this is mzj's test!");
		document.add(a);
		
		Paragraph title1 = new Paragraph("chapter1");
		Chapter chapter1 = new Chapter(title1,1);
		Paragraph title2 = new Paragraph("chapter1.section1");
		Section section1 =  chapter1.addSection(title2,1);
		document.add(chapter1);
		
		PdfPTable table = new PdfPTable(3);
		PdfPCell cell;
		cell = new PdfPCell(new Phrase("Cell with colspan 3"));
		cell.setColspan(3);
		table.addCell(cell);
		cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
		cell.setRowspan(2);
		table.addCell(cell);
		table.addCell("row 1; cell 1");
		table.addCell("row 1; cell 2");
		table.addCell("row 2; cell 1");
		table.addCell("row 2; cell 2");
		document.add(table);
		
		Image jpg = Image.getInstance("1.jpg");
		jpg.scalePercent(50);
		document.add(jpg);
		Image gif = Image.getInstance("1.gif");
		document.add(gif);
	}

	public static void main(String[] args) {
//		generatePdf("IText.pdf");
		xml2pdf();
	}
}
